home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / xlib / animate.c.z / animate.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.2 KB  |  147 lines

  1. /*
  2.  * animate - simple double buffered RGBA xlib program which rotates an object.
  3.  */
  4. /* compile: cc -o animate animate.c -lGLU -lGL -lX11 */
  5.  
  6. #include <GL/glx.h>
  7. #include <GL/glu.h>
  8. #include <X11/keysym.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
  13.  
  14. static Bool animate;
  15.  
  16. static void
  17. initialize(void) {
  18.     glShadeModel(GL_FLAT);
  19.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  20.     gluPerspective(40., 3.0/2.0, 0.001, 100000.0);
  21.     glTranslatef(0.0, 0.0, -3.0);
  22.     glClearColor(0.2,0.2,0.2,0.);
  23. }
  24.  
  25. static void
  26. side(void) { /* make a square translated 0.5 in the z direction */
  27.     glPushMatrix();
  28.     glTranslatef(0.0,0.0,0.5);
  29.     glRectf(-0.5,-0.5,0.5,0.5);
  30.     glPopMatrix();
  31. }
  32.  
  33. static void
  34. cube(void) { /* make a cube out of 4 squares */
  35.     glPushMatrix();
  36.     side();
  37.     glRotatef(90.,1.,0.,0.);
  38.     side();
  39.     glRotatef(90.,1.,0.,0.);
  40.     side();
  41.     glRotatef(90.,1.,0.,0.);
  42.     side();
  43.     glPopMatrix();
  44. }
  45.  
  46. static void
  47. draw_scene(void) {
  48.     static float rot = 0.;
  49.  
  50.     glClear(GL_COLOR_BUFFER_BIT);
  51.     glColor3f(.1, .1, .8);
  52.     glPushMatrix();
  53.     if (animate && (rot += 5.) > 360.) rot -= 360.;
  54.     glRotatef(rot,0.,1.,0.);
  55.     cube();
  56.     glScalef(0.3,0.3,0.3);
  57.     glColor3f(.8, .8, .1);
  58.     cube();
  59.     glPopMatrix();
  60. }
  61.  
  62. static Bool
  63. process_input(Display *dpy) {
  64.     XEvent event;
  65.     static Bool mapped;
  66.     Bool redraw = 0;
  67.  
  68.     if(XPending(dpy) || !mapped || !animate) {
  69.     char buf[31];
  70.     KeySym keysym;
  71.  
  72.     XNextEvent(dpy, &event);
  73.     switch(event.type) {
  74.     case Expose:
  75.         redraw = 1;
  76.         break;
  77.     case ConfigureNotify:
  78.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  79.         redraw = 1;
  80.         break;
  81.     case MapNotify:
  82.         mapped = 1; break;
  83.     case UnmapNotify:
  84.         mapped = 0; break;
  85.     case KeyPress:
  86.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  87.         switch (keysym) {
  88.         case XK_Escape:
  89.         exit(EXIT_SUCCESS);
  90.         case XK_space:
  91.         animate ^= 1; break;
  92.         default:
  93.         break;
  94.         }
  95.     default:
  96.         break;
  97.     }
  98.     }
  99.     return redraw | animate;
  100. }
  101.  
  102. int
  103. main(int argc, char **argv) {
  104.     Display *dpy;
  105.     XVisualInfo *vi;
  106.     XSetWindowAttributes swa;
  107.     Window win;
  108.     GLXContext cx;
  109.     Bool isdirect;
  110.  
  111.     dpy = XOpenDisplay(0);
  112.  
  113.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  114.     fprintf(stderr, "animate: no suitable RGB visual available\n");
  115.     exit(EXIT_FAILURE);
  116.     }
  117.  
  118.     /* create a GLX context */
  119.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  120.     isdirect = glXIsDirect(dpy, cx);
  121.  
  122.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  123.                                    vi->visual, AllocNone);
  124.  
  125.     swa.border_pixel = 0;
  126.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  127.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300,
  128.             0, vi->depth, InputOutput, vi->visual,
  129.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  130.     XStoreName(dpy, win, "animate");
  131.     XMapWindow(dpy, win);
  132.  
  133.     glXMakeCurrent(dpy, win, cx);
  134.  
  135.     initialize();
  136.  
  137.     printf("hit 'spacebar' to toggle animation\n");
  138.  
  139.     while (1) {
  140.     if (process_input(dpy)) {
  141.         draw_scene();
  142.         glXSwapBuffers(dpy, win);
  143.         if (!isdirect) glFinish();
  144.     }
  145.     }
  146. }
  147.